package com.woniuxy.rediscache.service.impl;

import com.woniuxy.rediscache.entity.Student;
import com.woniuxy.rediscache.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * description: StudentServiceImpl <br>
 * date: 2022/1/6 12:38 上午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    //key student:1    student:2

    //value:命名空间
    //key:键的名字，遵守SpringExpressionLanguage
    //#参数名
    //@Cacheable(value = "student",key ="#sid" )
    //@Cacheable(value = "student",key ="#p0" )
    //@Cacheable(value = "student",key ="#a0" )
    //#root根节点
    //@Cacheable(value = "student",key ="#root.methodName+':'+#sid" )
    //@Cacheable(value = "student",key ="methodName+':'+#sid" )
    //满足条件的数据才缓存
    @Cacheable(value = "student", key = "#sid", condition = "#sid>1")
    @Override
    public Student getStudentBySid(int sid) {
        Student student = null;
        //查询数据，先判断缓存中是否存在
        //缓存没有，数据库查询，存入缓存,，缓存中有，缓存读取

        System.out.println("数据库查询,存储缓存:" + sid);
        if (sid == 1) {
            student = new Student(1, "zs");
        } else {
            student = new Student(2, "ls");
        }
        return student;
    }

    // key studentpage:1:10    studentpage:2:10
    //多条件组合
    @Override
    public List<Student> getStudentsByPage(int pageIndex, int pageSize) {
        ArrayList<Student> allStudents = new ArrayList<>();
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        for (int i = 1; i < 100; i++) {
            allStudents.add(new Student(i, "name" + i));
        }
        List<Student> students = new ArrayList<>();
        if (!redisTemplate.hasKey("studentspage:" + pageIndex + ":" + pageSize)) {
            System.out.println("查询数据库,存储缓存");
            for (int i = (pageIndex - 1) * pageSize; i < pageIndex * pageSize; i++) {
                students.add(allStudents.get(i));
            }
            opsForValue.set("studentspage:" + pageIndex + ":" + pageSize, students);
        } else {
            System.out.println("缓存查询");
            students = (List<Student>) opsForValue.get("studentspage:" + pageIndex + ":" + pageSize);
        }
        return students;
    }

    @Override
    @CachePut(value = "student", key = "#student.sid")
    public Student addStudent(Student student) {
        System.out.println("执行数据库添加：" + student);
        return student;
    }

    @Override
    @CachePut(value = "student", key = "#student.sid")
    public Student modifyStudent(Student student) {
        System.out.println("执行数据修改：" + student);
        return student;
    }


    @Override
    @CacheEvict(value = "student", key = "#sid", beforeInvocation = false)
    public void removeStudent(int sid) {
        System.out.println("执行数据库删除" + sid);
        throw new RuntimeException();
    }

}
